package com.atguigu.springcloud.alibaba.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

@Slf4j
@RestController
public class DemoController {

    @Autowired
    RestTemplate restTemplate1;

    @Value(value = "${server.port}")
    String port;

    private String serviceName = "nacos-zengbang";

    @GetMapping(value = "/demo1")
    public String demo1(@RequestParam("serviceName") String serviceName, @RequestParam("port") String port) {
        HttpEntity entity = new HttpEntity<>(null, null);
        ResponseEntity<String> exchange = restTemplate1.exchange("http://" + serviceName + "/answer", HttpMethod.GET, entity, String.class);
        return exchange.getBody();
    }

    @GetMapping(value = "/answer")
    public String result() {
        return String.format("访问成功！这是端口%s，时间是%s", port, new Date().toLocaleString());
    }
}
